import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NotarizationInfoService } from '../../../services/notarization-info.service';
import { HttpService } from '../../../services/http.service';
import { LoginInfoService } from '../../../services/login-info.service';
import { BaseUiService } from '../../../services/base-ui.service';
import {CityDataService} from "../../../services/city-data.service";

@Component({
  selector: 'app-applyinfo',
  templateUrl: './applyinfo.page.html',
  styleUrls: ['./applyinfo.page.scss'],
})
export class ApplyinfoPage implements OnInit {
  public showBackdrop = false;
  public infoClass:boolean = false;
 
  public proposerList = [] //申请人列表
  public useCountry = ''; //选择的地区
  public useLanguage = '';//选择的语言
  public languageList:any //语言列表
  public countryList:any //公证处列表
  public userInfo:any //当前登录人的信息
  public isDaiban:number //是否代办 1是 0否
  public matterInfo:any //选择的公证事项
  public sexlist:any = [
    {
      name:'男',
      value: 1
    },
    {
      name:'女',
      value: 0
    }
  ]
  public cityColumns: any[];
  constructor(
    public baseUiService: BaseUiService,
    public router: Router,
    public NotarizationInfoService:NotarizationInfoService,
    public HttpService: HttpService,
    public LoginInfoService: LoginInfoService,
    public CityDataService: CityDataService
  ) {
    this.cityColumns = this.CityDataService.cities;
   }

  ngOnInit() {
    this.seach()
    this.userInfo = this.LoginInfoService.getlocal("user")
    this.isDaiban = !!this.NotarizationInfoService.getisDaiBan() ? this.NotarizationInfoService.getisDaiBan() : '0'; 
    if(this.isDaiban == 1){
      this.addClick()
      if(this.userInfo.gender == 1){
        this.userInfo.gender = '男'
      }else{
        this.userInfo.gender = '女'
      }
    }else{
      this.proposerList.push(this.userInfo)
    }
    
    this.matterInfo = this.NotarizationInfoService.getNotayMatter()
  }
  
  //页面刷新
  seach(){
    this.HttpService.getCountry({}).then(
      res => {
        this.countryList = res['items']
      }
    )
    this.HttpService.getLanguage({}).then(res =>{
      if(res['code'] == 200){
        this.languageList = res['items']
      }else{
        this.baseUiService.showToast(res['msg'],'danger','top')
      }
    }).catch(err =>{
      this.baseUiService.showToast(err,'danger','top')
    })
  }
  //添加申请人
  addClick(){
    let info = {
      userName:'',
      gender:'',
      birthday:'',
      idCard :'',
      mobile:'',
    }
    this.proposerList.push(info)
  }
  //下一步
  goConfirm(){
    console.log(11111)
      for(let item of this.proposerList){
        console.log(this.baseUiService.nameVerify(item.name))
        if (item.userName == "" || !this.baseUiService.nameVerify(item.userName)) {
          this.baseUiService.showToast('请输入正确的姓名','warning','top')
          return;
        }else if (this.useCountry == "" ){
          this.baseUiService.showToast('请选择使用地区','warning','top')
          return;
        }else if (this.useLanguage == "" ){
          this.baseUiService.showToast('请选择公证书译文语言','warning','top')
          return;
        }else if(item.gender === ""){
          this.baseUiService.showToast('请选择性别','warning','top')
          return;
        }else if (item.birthday == "" ){
          this.baseUiService.showToast('请选择出生日期','warning','top')
          return;
        }else if (item.idCard == "" || !this.baseUiService.idCardVerify(item.idCard)){
          this.baseUiService.showToast('请输入正确的身份证号','warning','top')
          return;
        }else{
          console.log(44444)
        }
      }
      
        this.NotarizationInfoService.setTransactor(this.proposerList)
          let p = {
            userId:this.userInfo.unitGuid,
            name:this.userInfo.userName,
            useArea: this.useCountry,
            useLanguage:this.useLanguage,
            purposeName:this.NotarizationInfoService.getNotayUse(),
            notaryId:this.NotarizationInfoService.getNotayId(),
            isDaiBan: this.isDaiban,
            notaryForm:1,
            description:"",
            notaryitems:JSON.stringify(this.matterInfo),
          }
          if(this.isDaiban === 0){
            p['applyUsers'] = JSON.stringify([{
              name: this.LoginInfoService.getInfo().userName,
              gender: this.LoginInfoService.getInfo().gender,
              birthday: this.LoginInfoService.getInfo().birthday,
              idCard: this.LoginInfoService.getInfo().idCard,
              mobile: this.LoginInfoService.getInfo().mobile
            }])
          }else{
            for(let item of this.proposerList){
              item['name'] = item.userName;
            }
            p['applyUsers'] = JSON.stringify(this.proposerList)
          }
          console.log(p,'+++666')
          this.HttpService.addNotaryorder(p).then(res => {
            console.log(res,'+++666')
            if(res['code'] == 200){
              this.router.navigate(['/transaction/confirminfo'])
              this.NotarizationInfoService.setOrder(res['unitGuid'])
            }else{
              this.baseUiService.showToast(res['msg'],'danger','top')
            }
          }).catch(err =>{
            this.baseUiService.showToast(err,'danger','top')
          })
  }
}
